<?php
/**
 * Atlas KOTASOFT cakephp
 *
 * Bakes web interface 
 *
 * PHP versions 4 and 5
 *
 * ATLAS KOTASOFT  cakephp web code generator
 * Copyright 2009, Kotasoft agile factory, Inc. (http://www.atlascakephp.com)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2009, Kotasoft agile factory , Inc. (http://www.kotasoft.com)
 * @link          http://www.atlascakephp.com  ATLAS KOTASOFT Project
 * @version       BETA 0.5 $
 * @modifiedby    $LastChangedBy: daniel castanera - guldoe and francisco dominguez - idioto $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

class UsersController extends AppController {
	 
    var $name = "Users"; 
    var $ruta_admin = "/admin/users/";
    var $uses  = array('Role');
    var $userDbConfig=KT_USERDBCONFIG;
    var $belongsTo = array('Role');
    
	var $paginate = array(
	    'User'    => array(
	        'limit'    => 10,
	        'page'    => 1,
	        'order'    => array('User.created'    => 'asc')
	    )
	); 
	
    
    /*para indexar buscador*/
	function indexData() {
                $index = $this->data['User']['username'];
                return $index;
        }
    
    /* list of users */    
    function admin_index() {   
    	$this->set('ruta_admin',$this->ruta_admin);
    	$this->set('users',$this->paginate("User"));
    }
    
    
    /* view user  info */
    function admin_view($id=null){
    	$this->set('ruta_admin',$this->ruta_admin);  	
    	$this->User->id=$id;
    	$this->set('users',$this->User->read()); 
    	$this->log(__('view user',true), LOG_DEBUG);	
		   	
    }
    
    /* search for user by name */
    function admin_search() {
    	$this->set('ruta_admin',$this->ruta_admin);
    	if (!empty($this->data)){
    		$value = $this->data['User']['username']; 
       		 $this->set('users',$this->User->findAllByUsername($value));
    	} 
    } 
    
    /* edit user info */
    function admin_edit($id=null){ 
    	echo "<pre>";print_r($this->User); echo "</pre>";    die();
    	$this->set('ruta_admin',$this->ruta_admin);
    	$this->set('roles',$this->__roleUser()); 
    	$this->User->id = $id;
    	 
    	if(empty($this->data)){
    		$this->data = $this->User->read(); 
    	} else {
    		if ($this->User->save($this->data)){
    			$this->Session->setFlash('usuario actualizado');
    				$this->log(__('usuario actualizado',true), LOG_DEBUG);	
    			$this->redirect(array('action'=>'index'));
    		}else{
    			$this->Session->setFlash('error actualizacion, compruebe los datos');
    			$this->log(__('error actualizacion, compruebe los datos',true), LOG_DEBUG);	
    		}
    	} 
    }
    
     /* delete user */
    function admin_delete($id=null) {
    	$this->User->del($id);
    	$this->Session->setFlash("Usuario eliminado");
    	$this->log(__('Usuario eliminado',true), LOG_DEBUG);	
    	$this->redirect(array('action'=>'admin_index'));    	
    }
    
    /* add new user */
    function admin_add() { 
    	$this->set('roles',$this->__roleUser()); 
    	$this->set('ruta_admin',$this->ruta_admin);
      	if (!empty($this->data)) {
    	    $this->data["User"]["password"]= md5($this->data["User"]["password"]);
    		if ($this->User->save($this->data)){
    			$this->Session->setFlash("Usuario insertado en la base de datos");
    			$this->log(__('Usuario insertado en la base de datos',true), LOG_DEBUG);	
    			$this->redirect(array('action' => 'admin_index'));
    		}else {
    			$this->Session->setFlash("Error en la insercion");
    			
    		}
    	} 
    }
    
    
    
    /*check if is in login */
    function beforeFilter() {     	 
    	if (!$this->Session->check('User') && 
    		isset($this->params['prefix']) && 
    		$this->params['prefix']=='admin') {	   		
				$this->checkAdminSession();
		}
    } 

     /* login function */
    function login() {    	
    	if ($this->Session->check('User')){
            $this->redirect('/admin/');
            exit();
        }
         
        
        
        $idioma =$this->data['User']['idiom'];
        
        if ($idioma==""){
            $this->Session->write('Config.language', "spa"); 
        	
        }else {
             $this->Session->write('Config.language', $idioma); 
        }
    	$this->layout ='login_layout'; 
        $this->set('error', false);

        // If a user has submitted form 
        if (!empty($this->data)) { 
        	$someone = $this->User->findByUsername($this->data['User']['username']);
        	
            if(!empty($someone['User']['password']) && ($someone['User']['password'] == md5($this->data['User']['password']))) {               
                $this->Session->write('User', $someone['User']);
                $this->redirect('/admin/');
                
            } else {
                 
                $this->Session->setFlash(__('Error login',true));
                $this->set('error', true);
            }
        } 
    }

     /* logout  function */
    function logout() {
      $this->Session->delete('User');
      $this->redirect('/');
    }
     
     /* get Roles for and user  */  
    /* deprecated */  
	function __roleUser() {				
		$role = new Role();
		$roles= $role->find('all');
		 
	    $array= Array();
		foreach ($roles as $aux){
			$array[$aux['Role']["id"]]= $aux['Role']["name"];			
		}  
		return $array;	
	}     
} 
?>